/**
 * Created by liubin on 2015/09/30.
 */

'use strict';
import Request from 'superagent-bluebird-promise';

export const SELECT_COMBO = 'selectRoleCombo';
export const SELECT_ROLE_LIST_PAGING = 'selectRoleListPaging';
export const ADD_ROLE = 'addRole';
export const REMOVE_ROLE = 'removeRole';
export const GET_ROLE = 'getRole';
export const MODIFY_ROLE = 'modifyRole';

function selectComboResult(json, callback) {
    return {
        type: SELECT_COMBO,
        data: json.data,
        status: json.status,
        callback
    };
}
/**
 * 下拉框查询
 * @param callback
 * @returns {{type: string, payload: {promise: *, resolve: Function}}}
 */
export function selectCombo(callback) {
    const promise = Request.get('/api/role').promise();
    return {
        type: SELECT_COMBO,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(selectComboResult(resp.body, callback));
            }
        }
    };
}

/**
 * 分页查询回调
 * @param json
 * @param callback
 * @returns {{type: string, status: *, data: *, draw: *, total: *, callback: *}}
 */
function selectResult(json, callback) {
    return {
        type: SELECT_ROLE_LIST_PAGING,
        status: json.status,
        data: json.data,
        draw: json.draw,
        total: json.total,
        callback
    };
}

/**
 * 分页查询
 * @param request
 * @param drawCallback
 * @param settings
 */
export function selectRoleListPaging(request, drawCallback, settings) {
    // 需要4个参数 查询值，排序字段，正序倒序,draw
    const draw = request.draw;
    const query = request.search.value;
    const dir = request.order[0] && request.order[0].dir || '';
    const order = request.order[0] && request.columns[request.order[0].column].data || '';
    const size = request.length;
    const start = request.start;
    const promise = Request.get('/api/role').query({
        draw,
        query,
        dir,
        order,
        size,
        start
    }).promise();
    return {
        type: SELECT_ROLE_LIST_PAGING,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(selectResult(resp.body, drawCallback));
            }
        }
    };
}
/**
 * 追加回调
 * @param json
 * @param callback
 * @returns {{type: ADD_USER, status: *, callback: *}}
 */
function addResult(json, callback) {
    return {
        type: ADD_ROLE,
        status: json.status,
        callback
    };
}
/**
 * 追加
 * @param data
 * @param callback
 * @returns {{type: string, payload: {promise: *, resolve: Function}}}
 */
export function addRole(data, callback) {
    const promise = Request.post('/api/role').send(data).promise();
    return {
        type: ADD_ROLE,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(addResult(resp.body, callback));
            }
        }
    };
}

/**
 * 删除权限后回调
 * @param json
 * @param callback
 * @returns {{type: string, status: *, callback: *}}
 */
function removeResult(json, callback) {
    return {
        type: REMOVE_ROLE,
        status: json.status,
        callback
    };
}
/**
 * 删除权限
 * @param id
 * @param callback
 * @returns {{type: REMOVE_USER, payload: {promise: *, resolve: Function}}}
 */
export function removeRole(id, callback) {
    const promise = Request.del(`/api/role/${id}`).promise();
    return {
        type: REMOVE_ROLE,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(removeResult(resp.body, callback));
            }
        }
    };
}

/**
 *
 * @param json
 * @param callback
 * @returns {{type: string, status: *, data: *, callback: *}}
 */
function getResult(json, callback) {
    return {
        type: GET_ROLE,
        status: json.status,
        data: json.data,
        callback
    };
}
/**
 * 查询单个权限
 * @param id
 * @param callback
 * @returns {{type: string, payload: {promise: *, resolve: Function}}}
 */
export function getRole(id, callback) {
    const promise = Request.get(`/api/role/${id}`).promise();
    return {
        type: GET_ROLE,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(getResult(resp.body, callback));
            }
        }
    };
}

/**
 * 修改权限的回调
 * @param json
 * @param data
 * @param d
 * @returns {{type: string, status: *, message: *, data: *, d: *}}
 */
function modifyResult(json, data, d) {
    return {
        type: MODIFY_ROLE,
        status: json.status,
        message: json.message,
        data,
        d
    };
}
/**
 * 修改权限
 * @param data
 * @param d
 * @returns {{type: string, payload: {promise: Bluebird.Promise, resolve: Function}}}
 */
export function modifyRole(data, d) {
    const role = data.toJS();
    const promise = Request.put(`/api/role/${role.id}`).send(role).promise();
    return {
        type: MODIFY_ROLE,
        payload: {
            promise,
            resolve: (resp, dispatch) => {
                dispatch(modifyResult(resp.body, data, d));
            },
            reject: (resp, dispatch) => {
                dispatch(modifyResult(resp.body, data, d));
            }
        }
    };
}